package com.system.server;

import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDAO {

    public static int User_number=0;
    //将一个用户的基本信息插入数据库中
    public static void insert(User user){
        String sql="insert into `user`(user_name,user_password,user_email) values('"+user.getUser_name()+"','"+user.getUser_password()+"','"+user.getUser_email()+"')";
        DataBaseControl.execute(sql);
        User_number++;
        return;
    }

    public static ResultSet selectAllUser(){
        String sql="select * from `user`";
        return DataBaseControl.execute(sql);
    }
    //通过用户名查询一个用户的信息
    public static ResultSet selectWithName(String name){
        String sql="select * from `user` where user_name='"+name+"'";
        return DataBaseControl.execute(sql);
    }
    //通过用户名更改密码
    public static void updatePassword(String name,String newPassword){
        String sql="update `user` set user_password='"+newPassword+"'where user_name='"+name+"'";
        DataBaseControl.execute(sql);
        return;
    }
    //通过旧邮箱更改邮箱
    public static void updateEmail(String email,String newEmail){
        String sql="update `user` set user_email='"+newEmail+"'where user_email='"+email+"'";
        DataBaseControl.execute(sql);
        return;
    }
    //更改签名
    public static void updateText(String newText,String userName){
        String sql="update `user` set user_text='"+newText+"'where user_name='"+userName+"'";
        DataBaseControl.execute(sql);
        return;
    }
    //更改地址
    public static void updateAddress(String newAddress,String userName){
        String sql="update `user` set user_address='"+newAddress+"'where user_name='"+userName+"'";
        DataBaseControl.execute(sql);
        return;
    }
    //充值操作
    public static void updateMoney(String newMoney,String userName) throws SQLException{
        String sql1="select user_money from `user` where user_name='"+userName+"'";
        ResultSet res=DataBaseControl.execute(sql1);
        double money = 0;
        while(res.next()){
            money=Double.parseDouble(res.getString("user_money"))+Double.parseDouble(newMoney);
        }
        String insertMoney=Double.toString(money);
        String sql2="update `user` set user_money='"+insertMoney+"'where user_name='"+userName+"'";
        DataBaseControl.execute(sql2);
        return;
    }
    //改昵称
    public static void updateNickName(String nickName,String userName){
        String sql="update `user` set user_nickName='"+nickName+"'where user_name='"+userName+"'";
        DataBaseControl.execute(sql);
        return;
    }

    //
    public static String getShopCarNum(String userName){
        String sql="select nowFoodNum from `user` where user_name='"+userName+"'";
        ResultSet res=DataBaseControl.execute(sql);
        try{
            while(res.next()) {
                return res.getString("nowFoodNum");
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return "ERROR";
    }

    //
    public static void addFoodToCar(String userName,String foodName,String currentNum){
        String str="food_"+currentNum;
        String sql1="update `user` set "+str+"='"+foodName+"' where user_name='"+userName+"'";
        String sql2="update `user` set nowFoodNum='"+currentNum+"' where user_name='"+userName+"'";
        DataBaseControl.execute(sql1);
        DataBaseControl.execute(sql2);
        return;
    }

    //
    public static String getCarFoodOfName(String userName,String CarFoodNum){
        String str="food_"+CarFoodNum;
        String sql="select "+str+" from `user` where user_name='"+userName+"'";
        ResultSet res=DataBaseControl.execute(sql);
        try{
            while(res.next()){
                return res.getString(str);
            }

        }catch (SQLException e){
            e.printStackTrace();
        }
        return "ERROR";
    }

    //
    public static void deleteForShopCar(String userName,String foodName){
        ResultSet res=selectWithName(userName);
        try{
            while(res.next()){
                for(int i=0;i<8;i++){
                    String str="food_"+(i+1);
                    System.out.println(res.getString(str));
                    if(res.getString(str).equals(foodName)){
                        System.out.println(str);
                        String sql="update `user` set "+str+"='0' where user_name='"+userName+"'";
                        DataBaseControl.execute(sql);
                        return;
                    }
                }
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return;
    }
}

